package edu.fjnu.order.view;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EtchedBorder;

import edu.fjnu.order.model.Employee;
import edu.fjnu.order.view.adapter.JListAdapter;
import edu.fjnu.order.view.adapter.JListCell;
import edu.fjnu.order.view.adapter.ManagerPanel;

@SuppressWarnings("serial")
public class EmployeeManagePanel extends ManagerPanel<Employee>{
	
	/***Start*******************界面控件*******************Start***/
	private JList employeeList;

	private JTextField employeeNoField;
	private JTextField employeeNameField;
	private JTextField employeeSexField;
	private JTextField employeeBirthdayField;
	
	private JTextField employeeAddressField;
	private JTextField employeeTelephoneField;
	private JTextField employeeHiredateField;
	private JTextField employeeDepartmentField;
	private JTextField employeeHeadshipField;
	private JTextField employeeSalaryField;
	
	private JTable employeeReportTable;

	private JButton addEmployeeBtn;
	private JButton updateEmployeeBtn;
	private JButton deleteEmployeeBtn;
	/***End**********************界面控件*********************End***/

	protected void init() {
		createWidget();
		setLayout(new BorderLayout());
		
		add(createList(), BorderLayout.WEST);
		add(createCenterJPanel(), BorderLayout.CENTER);
		
		//TODO 测试用的
		test();
	}

	private void createWidget() {
		employeeNoField = new JTextField();
		employeeNameField = new JTextField();
		employeeSexField = new JTextField();
		employeeBirthdayField = new JTextField();
		
		employeeAddressField = new JTextField();
		employeeTelephoneField = new JTextField();
		employeeHiredateField = new JTextField();
		employeeDepartmentField = new JTextField();
		employeeHeadshipField = new JTextField();
		employeeSalaryField = new JTextField();
		
		employeeReportTable = new JTable(new String [][]{{"aa", "bb", "cc"}, {"ee", "ff", "gg"}}, new String [] {"11", "22", "33"});

		addEmployeeBtn = new JButton("添加");
		updateEmployeeBtn = new JButton("修改");
		deleteEmployeeBtn = new JButton("删除");
	}

	private void test() {
		List<Employee> list = listAdapter.getList();
		//TODO 测试用的 
		for(int i = 0; i < 200; i++){
			Employee e = new Employee();
			e.setName("name" + i);
			list.add(e);
		}
	}

	private JScrollPane createList() {
		listAdapter = new JListAdapter<Employee>();
		employeeList = new JList(listAdapter);
		//设置单选
		employeeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		employeeList.setCellRenderer(new JListCell());
		return new JScrollPane(employeeList);
	}
	
	private JPanel createCenterJPanel(){
		JPanel centerPanel = new JPanel(new BorderLayout());
		
		JPanel northPanel = new JPanel();
		BoxLayout northLayout = new BoxLayout(northPanel, BoxLayout.Y_AXIS);
		northPanel.setLayout(northLayout);
		northPanel.add(creatBaseInfoPanel());
		northPanel.add(createMoreInfoPanel());
		
		centerPanel.add(northPanel, BorderLayout.NORTH);
		centerPanel.add(createSouthPanel(), BorderLayout.SOUTH);
		centerPanel.add(createReportPanel(), BorderLayout.CENTER);
		return centerPanel;
	}
	

	/**
	 * 创建更多信息面板
	 */
	private JPanel createMoreInfoPanel() {
		JPanel moreInfoPanel = new JPanel();
		BoxLayout moreInfoLayout = new BoxLayout(moreInfoPanel, BoxLayout.X_AXIS);
		moreInfoPanel.setLayout(moreInfoLayout);
		//TODO 标题 - 硬编码
		JPanel west = new JPanel();
		BoxLayout westLayout = new BoxLayout(west, BoxLayout.Y_AXIS);
		west.setLayout(westLayout);
		west.add(createConsistPanel("联系地址  : ", employeeAddressField));
		west.add(createConsistPanel("联系电话  : ", employeeTelephoneField));
		west.add(createConsistPanel("雇佣日期  : ", employeeHiredateField));
		
		JPanel east = new JPanel();
		BoxLayout eastLayout = new BoxLayout(east, BoxLayout.Y_AXIS);
		east.setLayout(eastLayout);
		east.add(createConsistPanel("　部门　 : ", employeeDepartmentField));
		east.add(createConsistPanel("　职务　 : ", employeeHeadshipField));
		east.add(createConsistPanel("　薪水　 : ", employeeSalaryField));
		
		moreInfoPanel.add(west);
		moreInfoPanel.add(east);
		moreInfoPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "更多信息"));
		return moreInfoPanel;
	}

	/**
	 * 创建基本信息面板
	 */
	private JPanel creatBaseInfoPanel(){
		JPanel baseInfoPanel = new JPanel();
		BoxLayout baseInfoLayout = new BoxLayout(baseInfoPanel, BoxLayout.X_AXIS);
		baseInfoPanel.setLayout(baseInfoLayout);
		//TODO 标题 - 硬编码
		JPanel west = new JPanel();
		BoxLayout westLayout = new BoxLayout(west, BoxLayout.Y_AXIS);
		west.setLayout(westLayout);
		west.add(createConsistPanel("　员工号   : ", employeeNoField));
		west.add(createConsistPanel("员工姓名   : ", employeeNameField));
		
		JPanel east = new JPanel();
		BoxLayout eastLayout = new BoxLayout(east, BoxLayout.Y_AXIS);
		east.setLayout(eastLayout);
		east.add(createConsistPanel("员工性别  : ", employeeSexField));
		east.add(createConsistPanel("员工生日  : ", employeeBirthdayField));
		
		baseInfoPanel.add(west);
		baseInfoPanel.add(east);
		baseInfoPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "基本信息"));
		return baseInfoPanel;
	}
	
	private JPanel createSouthPanel() {
		JPanel southPanel = new JPanel();
		southPanel.add(addEmployeeBtn);
		southPanel.add(updateEmployeeBtn);
		southPanel.add(deleteEmployeeBtn);
		return southPanel;
	}
	
	private JScrollPane createReportPanel() {
		JScrollPane reportPanel = new JScrollPane(employeeReportTable);
		reportPanel.setBorder(BorderFactory.createTitledBorder( 
				BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "员工业绩"));
		return reportPanel;
	}
}
